package org.yys.demo;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.yys.constant.MqConstant;

import java.io.IOException;
import java.util.List;

public class TagDemo {


    public void tagProducer() throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        // 创建生产者
        DefaultMQProducer producer = new DefaultMQProducer("tag-producer-group");
        producer.setNamesrvAddr(MqConstant.NAMESRV_ADDR);
        producer.start();

        for (int i = 0; i < 10; i++) {
            String tag = "tag" + (i % 2 + 1);

            // 创建消息
            Message message = new Message("tag-topic", tag, ("这是一个简单的消息. from tag:"+tag).getBytes());

            // 发送消息
            SendResult send = producer.send(message);
            System.out.println("result:"+send);
        }


        producer.shutdown();

    }

    public void tagConsumer1() throws MQClientException, InterruptedException, IOException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("tag1-consumer-group");
        consumer.setNamesrvAddr(MqConstant.NAMESRV_ADDR);
        consumer.subscribe("tag-topic","tag1");
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                System.out.println("消费者接收消息...tag1");
                System.out.println("message:"+new String(list.get(0).getBody()));
//                System.out.println("context:"+consumeConcurrentlyContext);
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });

        consumer.start();


        // 线程睡100s. 注:由于监听器在另一个线程中, 不会被当前线程的sleep影响.
        System.in.read();

    }

    public void tagConsumer2() throws MQClientException, InterruptedException, IOException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("tag2-consumer-group");
        consumer.setNamesrvAddr(MqConstant.NAMESRV_ADDR);
        consumer.subscribe("tag-topic","tag1 || tag2");
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                System.out.println("消费者接收消息...tag1||tag2");
                System.out.println("message:"+new String(list.get(0).getBody()));
//                System.out.println("context:"+consumeConcurrentlyContext);
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });

        consumer.start();


        // 线程睡100s. 注:由于监听器在另一个线程中, 不会被当前线程的sleep影响.
        System.in.read();

    }

}
